package com.wizardClient;

import Validator.Validator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.ClientsModel;
import javafx.event.ActionEvent;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

/**
 *
 * @author Sawior
 */
public class ChooseClientController implements Initializable, ClientControlledScreen {

    private ClientScreenController myController;
    @FXML
    private TextField name;
    @FXML
    private TextField lastName;
    @FXML
    private TextField city;
    @FXML
    private TextField zipCode;
    @FXML
    private TextField street;
    @FXML
    private TextField houseNr;
    @FXML
    private TextField localNr;
    @FXML
    private TextField nip;
    @FXML
    private TextField phone;
    @FXML
    private TextArea comment;
    @FXML
    private TextField email;
    @FXML
    private TextField startDate;
    @FXML
    private TextField endDate;
    @FXML
    private Label labelName;
    @FXML
    private Label labelLastName;
    @FXML
    private Label labelCity;
    @FXML
    private Label labelZipCode;
    @FXML
    private Label labelStreet;
    @FXML
    private Label labelHouseNr;
    @FXML
    private Label labelLocalNr;
    @FXML
    private Label labelNip;
    @FXML
    private Label labelPhone;
    @FXML
    private Label labelComment;
    @FXML
    private Label labelEmail;
    @FXML
    private Label labelStartDate;
    @FXML
    private Label labelEndDate;
    private ArrayList<Node> inputs = new ArrayList<Node>();
    private ArrayList<Label> labels = new ArrayList<Label>();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        loadActualDate();
        inputs.add(name);
        inputs.add(lastName);
        inputs.add(city);
        inputs.add(zipCode);
        inputs.add(street);
        inputs.add(houseNr);
        inputs.add(localNr);
        inputs.add(nip);
        inputs.add(phone);
        inputs.add(comment);
        inputs.add(email);
        inputs.add(startDate);
        inputs.add(endDate);

        labels.add(labelName);
        labels.add(labelLastName);
        labels.add(labelCity);
        labels.add(labelZipCode);
        labels.add(labelStreet);
        labels.add(labelHouseNr);
        labels.add(labelLocalNr);
        labels.add(labelNip);
        labels.add(labelPhone);
        labels.add(labelComment);
        labels.add(labelEmail);
        labels.add(labelStartDate);
        labels.add(labelEndDate);

    }

    private void loadActualDate() {
        Date actualDate = new Date();
        startDate.setText(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(actualDate));
        actualDate.setTime(actualDate.getTime() + 86400000);
        endDate.setText(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(actualDate));

    }

    @Override
    public boolean validateForm() {
        List<Boolean> checks = Validator.checkClientCheckList(getClient(), getStartDate(), getEndDate());
        setWrongTextFields(checks, isEndDateAfterStartDate(getStartDate(), getEndDate()));
        return isCorrect(checks) && isEndDateAfterStartDate(getStartDate(), getEndDate());
    }

    public void setWrongTextFields(List<Boolean> textFieldsChecks, boolean goodDates) {
        int i = 0;
        for (Boolean isGood : textFieldsChecks) {
            if (isGood) {
                if(inputs.get(i).getStyleClass().remove("textFieldWrong")) {
                    inputs.get(i).getStyleClass().add("textField");
                    labels.get(i).setText("");
                }
            } else {
                if(inputs.get(i).getStyleClass().remove("textField")){
                    inputs.get(i).getStyleClass().add("textFieldWrong");
                    labels.get(i).setText("Złe dane");
                }
            }
            i++;
        }
        if (goodDates) {
            startDate.getStyleClass().add("textField");
            endDate.getStyleClass().add("textField");
        } else {
            if(startDate.getStyleClass().add("textFieldWrong")) {
                endDate.getStyleClass().add("textFieldWrong");
            }
        }
    }

    public void setAllTextFieldsCorrect() {
        for (int i = 0; i < inputs.size(); i++) {
            inputs.get(i).getStyleClass().add("textField");
        }
    }

    public ClientsModel getClient() {
        String name = this.name.getText();
        String lastname = this.lastName.getText();
        String city = this.city.getText();
        String zipCode = this.zipCode.getText();
        String street = this.street.getText();
        String houseNumber = this.houseNr.getText();
        String nipNumber = this.nip.getText();
        Integer localNumber = null;
        try {
            localNumber = Integer.parseInt(this.localNr.getText());
        } catch (NumberFormatException ex) {
            Logger.getLogger(ChooseClientController.class.getName()).log(Level.SEVERE, "[CarPark] Exception while local number parsing.");
        }
        String phone = this.phone.getText();
        String comments = this.comment.getText();
        String email = this.email.getText();

        ClientsModel client = new ClientsModel(name, lastname, city, zipCode, street, houseNumber, nipNumber);
        client.setLocalNumber(localNumber);
        client.setPhone(phone);
        client.setComments(comments);
        client.setEmail(email);

        return client;
    }

    public Date getStartDate() {
        Date startDate = null;
        try {
            startDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(this.startDate.getText());
        } catch (ParseException ex) {
            Logger.getLogger(ChooseClientController.class.getName()).log(Level.SEVERE, "[CarPark] Exception while date formating - Client form start date");
        }

        return startDate;
    }

    public Date getEndDate() {
        Date endDate = null;
        try {
            endDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(this.endDate.getText());

        } catch (ParseException ex) {
            Logger.getLogger(ChooseClientController.class.getName()).log(Level.SEVERE, "[CarPark] Exception while date formating - Client form end date");
        }
        return endDate;
    }

    public boolean isEndDateAfterStartDate(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return false;
        }
        return endDate.after(startDate);
    }

    private boolean isCorrect(List<Boolean> list) {
        for (Boolean el : list) {
            if (!el) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void goToMain(ActionEvent event) {
        myController.setScreen(ClientScreensFramework.FIRST_SCREEN);
    }

    public void loadData(Integer selectedClient) {
        ClientsModel client = DaoInstanceEnum.INSTANCE.getDao().getClientById(selectedClient);
        if (client != null) {
            this.name.setText(client.getName());
            this.lastName.setText(client.getLastName());
            this.city.setText(client.getCity());
            this.zipCode.setText(client.getZipCode());
            this.street.setText(client.getStreet());
            this.houseNr.setText(client.getHouseNumber());
            Integer localNumber = client.getLocalNumber();
            if (localNumber != null) {
                this.localNr.setText(client.getLocalNumber().toString());
            }
            String clientPhone = client.getPhone();
            if (clientPhone != null) {
                this.phone.setText(client.getPhone());
            }
            String clientNip = client.getNipNumber();
            if (clientNip != null) {
                this.nip.setText(client.getNipNumber());
            }
        }
    }

    public void clearData() {
        this.name.setText("");
        this.lastName.setText("");
        this.city.setText("");
        this.zipCode.setText("");
        this.street.setText("");
        this.houseNr.setText("");
        this.localNr.setText("");
        this.phone.setText("");
        this.nip.setText("");
        this.comment.setText("");
        this.email.setText("");
    }
}
